#include"processbar.h"

#include<memory.h>

#include<unistd.h>

// 定义颜色代码
#define RED     "\033[31m"
#define GREEN   "\033[32m"
#define YELLOW  "\033[33m"
#define BLUE    "\033[34m"
#define RESET   "\033[0m"

//version1
//#define length 101
//#define Style '!'
//const char* lable = "|/-\\";
//
//void ProBar()
//{
//  char bar[length];
//  memset(bar,'\0',sizeof (bar));
//  int len = strlen(lable);
//
//  int cnt = 0;
//  while(cnt <= 100)
//  {
//    printf("[%-100s][%3d%%][%c]\r",bar,cnt,lable[cnt%len]);
//    fflush(stdout);
//
//    bar[cnt++] = Style;
//
//    usleep(20000);
//  }
//  printf("\ncompleted!\n");
//  
//}


//version2
#define Length 101
#define Style '#'
const char* lable = "|/-\\";

void ProBar(double current,double filesize)
{
  char bar[Length];
  memset(bar,'\0',sizeof (bar));

  int len = strlen(lable);

  double rate = current*100.0/filesize;
  int loop__count = (int)rate;
  int cnt = 0;


  while(cnt <= loop__count)
  {
    bar[cnt++] = Style;
  }

  printf("[%s%-100s%s][%s%.1lf%%%s][%c]\r",YELLOW,bar,RESET,RED,rate,RESET,lable[cnt%len]);
  fflush(stdout);
}
